//
//  AddOwnerViewController.m
//  Jyxt
//
//  Created by 刘日 on 17/6/1.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "AddOwnerViewController.h"
#import "OwnerInfoModel.h"
#import "HouseInfoGrabViewModel.h"
#import "LemonBestView.h"

@interface AddOwnerViewController ()

@property (nonatomic , strong) OwnerInfoModel *owner;
@property (nonatomic , strong) NSArray<NSString *> *typesNameArr;

@end

@implementation AddOwnerViewController

- (void)initView{
    [[[self.nameField.rac_textSignal merge: self.numberField.rac_textSignal] merge: self.typeLabel.rac_textSignal] subscribeNext:^(id  _Nullable x) {
        self.saveButton.enabled = (self.nameField.text.length > 0 && self.typeLabel.text.length > 0 && self.numberField.text.length > 0);
    }];
    self.typeArea.onTap = ^(UIView *view){
        [self selectType];
    };
    [[self.typeLabel rac_valuesForKeyPath: @"text" observer: self] subscribeNext:^(id  _Nullable x) {
        self.saveButton.enabled = (self.nameField.text.length > 0 && self.typeLabel.text.length > 0 && self.numberField.text.length > 0);
    }];
    [[self.saveButton rac_signalForControlEvents: UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        self.owner.name = self.nameField.text;
        self.owner.idNo = self.numberField.text;
        if (self.editIndex)
            [HouseInfoGrabViewModel owners][self.editIndex.integerValue] = self.owner;
        else
            [[HouseInfoGrabViewModel owners] addObject: self.owner];
        [LKFreeCtrl popViewController];
    }];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    if (self.editIndex){
        OwnerInfoModel *owner = [HouseInfoGrabViewModel owners][self.editIndex.integerValue];
        self.nameField.text = owner.name;
        self.typeLabel.text = self.typesNameArr[owner.idType.integerValue - 1];
        self.numberField.text = owner.idNo;
    }
}

- (OwnerInfoModel *)owner{
    if (!_owner) {
        _owner = [[OwnerInfoModel alloc] init];
    }
    return _owner;
}

- (NSArray<NSString *> *)typesNameArr{
    if (!_typesNameArr) {
        _typesNameArr = @[@"身份证", @"军人身份证件" , @"武装警察身份证件", @"港澳居民来往内地通行证" , @"台湾居民来往内地通行证" , @"护照"];
    }
    return _typesNameArr;
}

- (void)selectType{
    LemonBestView *as = [LemonBestView defaultActionSheetView];
    [as clear];
    as.headViewHeight = 44;
    as.title =  @"请您选择证件类型";
    LemonBestItem *cancel = [[LemonBestItem alloc] initWithTitle: @"取消" onTouchBlock:^(LemonBestItem *item) {
        [as hide];
    }];
    ((UILabel *)cancel.contentView).textColor = [UIColor redColor];
    [as addAction: cancel groupIndex: 0];
    [as addActionGroup];
    as.backViewControllerScale = 0.9;// 设置1 就没有后边viewController的缩放效果了
    for (NSInteger i = self.typesNameArr.count - 1; i >= 0;  i --) {
        [as addAction: [[LemonBestItem alloc] initWithTitle: self.typesNameArr[i] onTouchBlock:^(LemonBestItem *item) {
            self.owner.idType = [NSString stringWithFormat: @"%ld" , i + 1];
            self.typeLabel.text = self.typesNameArr[i];
            [as hide];
        }] groupIndex:1];
    }
    [as show];
}

@end
